﻿namespace GuiaTest
{
    using System;
    using Guia.Core;
    using Guia.Elements;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfTestApplication;

    /// <summary>
    /// Summary description for TabControlTest
    /// </summary>
    [TestClass]
    public class TabControlTest : AbstractWindowTest
    {
        [TestMethod]
        public void TestGetSelectedItem()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            TabItem tabItem1 = tabControl1.GetTab("Tab 1");
            tabItem1.Select();
            TabItem tabItem = tabControl1.SelectedTab;
            Assert.AreEqual("Tab 1",tabItem.Header);
        }

        [TestMethod]
        public void TestIsSelected()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            Assert.IsTrue(tabControl1.GetTab("Tab 1").IsSelected);
            tabControl1.GetTab("Tab 1").Select();
            Assert.IsTrue(tabControl1.GetTab("Tab 1").IsSelected);
            tabControl1.GetTab("Tab 3").Select();
            Delay.ForMilliseconds(200);
            Assert.IsFalse(tabControl1.GetTab("Tab 1").IsSelected);
            Assert.IsTrue(tabControl1.GetTab("Tab 3").IsSelected);
        }

        [TestMethod]
        public void TestGetSelectedItemNothingSelected()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl2");
            TabItem tabItem = tabControl1.SelectedTab;
            Assert.IsNull(tabItem);
        }

        [TestMethod]
        public void TestGetItemsIntIndexer()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            Assert.AreEqual("Tab 1", tabControl1[0].Header);
            Assert.AreEqual("Tab 2", tabControl1[1].Header);
            Assert.AreEqual("Tab 3", tabControl1[2].Header);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestGetItemsIntIndexerOutOfRangeTooSmall()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            TabItem temp = tabControl1[-1];
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestGetItemsIntIndexerOutOfRangeTooBig()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            TabItem temp = tabControl1[20];
        }

        [TestMethod]
        public void TestSelectItem()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            TabItem tabItem1 = tabControl1.GetTab("Tab 1");
            TabItem tabItem3 = tabControl1.GetTab("Tab 3");
            Label label1 = tabControl1.Get<Label>("label1");
            Assert.IsFalse(label1.IsOffscreen);
            tabItem3.Select();
            Label label3 = tabControl1.Get<Label>("label3");
            Assert.IsFalse(label3.IsOffscreen);
            Assert.IsTrue(label1.IsOffscreen);
            tabItem1.Select();
            Assert.IsFalse(label1.IsOffscreen);
            Assert.IsTrue(label3.IsOffscreen);
        }

        [TestMethod,ExpectedException(typeof(OperationNotSupportedException))]
        public void TestSelectDisabledItem()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            TabItem tabItem2 = tabControl1.GetTab("Tab 2");
            tabItem2.Select();
        }

        [TestMethod]
        public void TestGetAllTabs()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            TabItem[] tabItems = tabControl1.AllTabs;
            Assert.AreEqual(3, tabItems.Length);
            Assert.AreEqual("Tab 1",tabItems[0].Header);
            Assert.AreEqual("Tab 2", tabItems[1].Header);
            Assert.AreEqual("Tab 3", tabItems[2].Header);
        }

        [TestMethod]
        public void TestSelectItemWithStringIndexer()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            TabItem tabItem1 = tabControl1["Tab 1"];
            TabItem tabItem3 = tabControl1["Tab 3"];
            Label label1 = tabControl1.Get<Label>("label1");
            Assert.IsFalse(label1.IsOffscreen);
            tabItem3.Select();
            Label label3 = tabControl1.Get<Label>("label3");
            Assert.IsFalse(label3.IsOffscreen);
            Assert.IsTrue(label1.IsOffscreen);
            tabItem1.Select();
            Assert.IsFalse(label1.IsOffscreen);
            Assert.IsTrue(label3.IsOffscreen);
        }

        [TestMethod, ExpectedException(typeof(OperationNotSupportedException))]
        public void TestSelectDisabledItemWithStringIndexer()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            TabItem tabItem2 = tabControl1["Tab 2"];
            tabItem2.Select();
        }

        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
        public void TestGetItemWithStringIndexerNullValue()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            TabItem tabItem1 = tabControl1[null];
        }

        [TestMethod,ExpectedException(typeof(ElementNotFoundException))]
        public void TestSelectInvalidTab()
        {
            TabControl tabControl1 = this.window.Get<TabControl>("tabControl1");
            tabControl1["invalid Tab"].Select();
        }

        #region Overrides of AbstractWindowTest

        protected override Type TestWindowType()
        {
            return typeof (TabControlTestWindow);
        }

        protected override string TestWindowName()
        {
            return "TabControlTestWindow";
        }

        #endregion
    }
}
